package com.demo2do.fms.admin.web.controller.data.car;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.fms.admin.service.DataService;
import com.demo2do.fms.admin.utils.FormatUtils;
import com.demo2do.fms.admin.web.view.ExcelTemplateView;
import com.demo2do.fms.entity.data.car.CarType;

/**
 * 查询商品车车型代码
 * 
 * @author Wy
 *
 */
@Controller
@RequestMapping("/car-types")
@MenuSetting("menu-data")
public class CarTypeController {
    
    @Autowired 
    @Qualifier("dataService")
    private DataService<CarType> carTypeService;
    
    /**
     * 选择公司进行查询
     * 
     * @param page
     * @return
     */
    @MenuSetting("submenu-car-type")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView list(Page page){
        
        ModelAndView modelAndView = new ModelAndView("data/car/car-type-list");
        
        int totals = carTypeService.count(CarType.class);
        if(totals > 0){
            
            List<CarType> carTypes = carTypeService.listByPage(CarType.class, page.with(totals));
            
            modelAndView.addObject("page", page);
            modelAndView.addObject("carTypes", carTypes);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * 
     * 输出当前页记录 / 输出所有记录
     * @param page
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView export(Page page) {
        
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("data/car/car-type-export.xlsx"));
        
        // 1. list result
        int totals = carTypeService.count(CarType.class);
        if(totals > 0){
            List<CarType> carTypes = carTypeService.listByPage(CarType.class, page.with(totals));
            modelAndView.addObject("carTypes", carTypes);
        }
        
        // 2. format util
        FormatUtils fmt = new FormatUtils();
        modelAndView.addObject("fmt", fmt);
        
        return modelAndView;
        
    }
}
